import { defineApplicationConfig } from './packages/vite-config/src';

/**
 * 通用配置已经在 defineApplicationConfig 实现， callback 会合并覆盖默认，如果有更多的需要改动，请修改源码，或者自定义。避免不同的配置冲突
 */
export default defineApplicationConfig(function (env) {
  const {
    VITE_APP_BASE_API,
    VITE_APP_BASE_URL,
    VITE_APP_AUTO_API,
    VITE_APP_AUTO_URL,
    VITE_DEV_PORT,
    VITE_DEV_OPEN_BROWSER,
  } = env;

  return {
    overrides: {
      server: {
        port: Number(VITE_DEV_PORT || 80),
        host: true,
        open: Boolean(VITE_DEV_OPEN_BROWSER || true),
        proxy: {
          [VITE_APP_BASE_API]: {
            target: VITE_APP_BASE_URL,
            changeOrigin: true,
            rewrite: (p: string) => p.replace(/^\/dev-api/, ''),
          },
          [VITE_APP_AUTO_API]: {
            target: VITE_APP_AUTO_URL,
            changeOrigin: true,
            rewrite: (p: string) => p.replace(/^\/auto-api/, 'autoapi'),
          },
        },
      },
      css: {
        postcss: {
          plugins: [
            {
              postcssPlugin: 'internal:charset-removal',
              AtRule: {
                charset: (atRule) => {
                  if (atRule.name === 'charset') {
                    atRule.remove();
                  }
                },
              },
            },
          ],
        },
      },
    },
  };
});
